import {formatDate} from "@/utils/index"


// 时间格式转换过滤器
// 参数:
// timestamp: 毫秒时间戳
// format='yyyy-MM-dd hh:mm:ss'  （连接字符可自定义）
// unit 单位 默认s  可选s、ms
// 例:
// 1566268629 | formatDate('yyyy-MM-dd hh:mm:ss'); //2019-08-20 10:37:09
export function _formatDate(timestamp="", format='yyyy-MM-dd hh:mm:ss', unit='s'){
  console.log("timestamp",timestamp,parseInt(timestamp))
  if(isNaN(parseInt(timestamp))){
    return timestamp
  }else{
    return formatDate(unit==='s'?timestamp*1000:parseInt(timestamp), format)
  }
}

/**
 * Show plural label if time is plural number
 * @param {number} time
 * @param {string} label
 * @return {string}
 */
function pluralize(time, label) {
  if (time === 1) {
    return time + label;
  }
  return `${time + label}s`;
}

/**
 * 项目类型 type 转成 字符串中文名
 * @param {string} type
 * @return {string}
 */
export function typeToSting(type) {
  const typeObj = {
    wxshop: "自定义商城",
    vipcard: "vip品牌名片"
  };
  return typeObj[type] || "未知项目"
}

/**
 * @param {number} time
 */
export function timeAgo(time) {
  const between = Date.now() / 1000 - Number(time);

  if (between < 3600) {
    return pluralize(~~(between / 60), " minute");
  } if (between < 86400) {
    return pluralize(~~(between / 3600), " hour");
  }
  return pluralize(~~(between / 86400), " day");
}

/**
 * Number formatting
 * like 10000 => 10k
 * @param {number} num
 * @param {number} digits
 */
export function numberFormatter(num, digits) {
  const si = [
    { value: 1e18, symbol: "E" },
    { value: 1e15, symbol: "P" },
    { value: 1e12, symbol: "T" },
    { value: 1e9, symbol: "G" },
    { value: 1e6, symbol: "M" },
    { value: 1e3, symbol: "k" }
  ];

  for (let i = 0; i < si.length; i += 1) {
    if (num >= si[i].value) {
      return (
        (num / si[i].value)
          .toFixed(digits)
          .replace(/\.0+$|(\.[0-9]*[1-9])0+$/, "$1") + si[i].symbol
      );
    }
  }
  return num.toString();
}

/**
 * 10000 => "10,000"
 * @param {number} num
 */
export function toThousandFilter(num) {
  return (Number(num) || 0)
    .toString()
    .replace(/^-?\d+/g, (m) => m.replace(/(?=(?!\b)(\d{3})+$)/g, ","));
}

/**
 * Upper case first char
 * @param {String} string
 */
export function uppercaseFirst(string) {
  return string.charAt(0).toUpperCase() + string.slice(1);
}
